package cs340.stormtroopers.junitTests;

import cs340.stormtroopers.hypeerweb.HyPeerWeb;
import cs340.stormtroopers.hypeerweb.Node;
import junit.framework.TestCase;

public class HyPeerWebTests extends TestCase
{
	public void testHyPeerWeb()
	{
		Node n0 = new Node(0);
		Node n1 = new Node(1);
		Node n2 = new Node(2);
		Node n3 = new Node(3);
		Node n4 = new Node(4);
		n0.addNeighbor(n1);
		n0.addNeighbor(n2);
		n0.addNeighbor(n4);
		n1.addNeighbor(n0);
		n1.addNeighbor(n3);
		n2.addNeighbor(n0);
		n2.addNeighbor(n3);
		n3.addNeighbor(n2);
		n3.addNeighbor(n1);
		n4.addNeighbor(n0);
		n4.addDownPointer(n1);
		n4.addDownPointer(n2);
		n1.addUpPointer(n4);
		n2.addUpPointer(n4);

		HyPeerWeb hw = HyPeerWeb.getSingleton();
		hw.reload("complete_web.sqlite"); // so that we can choose the DB name
		hw.clear();
		hw.addNode(n0);
		hw.addNode(n1);
		hw.addNode(n2);
		hw.addNode(n3);
		hw.addNode(n4);
		hw.saveToDatabase();

		// now we test loading it!
		hw.clear();
		assertTrue(hw.size() == 0);
		hw.reload("complete_web.sqlite");
		assertTrue(hw.size() == 5);
		Node n3reloaded = hw.getNode(3);
		assertTrue(n3reloaded != null);
		Node n2reloaded = hw.getNode(2);
		assertTrue(n2reloaded != null);
		assertTrue(n2reloaded.getNeighbors().contains(n3reloaded));
	}
}
